<?php
/**
 * Created by PhpStorm.
 * User: dungnt13
 * Date: 11/24/14
 * Time: 11:55 AM
 */

namespace Admin;

use AdminController,
    Sentry,
    Input,
    Config,
    Validator,
    Redirect,
    View,
    Contest,
    Str,
    Lesson,
    Jinput;

class LessonsController extends AdminController
{
    public function getIndex()
    {
        $data['appends']['withTrashed'] = Jinput::get('withTrashed', false);
        $data['appends']['onlyTrashed'] = Jinput::get('onlyTrashed', false);
        $data['appends']['tone'] = Jinput::get('tone', 0);
        $data['appends']['category'] = Jinput::get('category', 0);
        $data['appends']['status'] = Jinput::get('status', 'published');

        $myModel = new \MyModel();

        $data['tones'] = $myModel->listAllItemsToArray("Tone", array('value' => 'id', 'display' => 'title'));
        $data['categories'] = $myModel->listAllItemsToArray("Category", array('value' => 'id', 'display' => 'title'));

        $arrParams = array();

        $arrParams['joins'][] = array('table' => 'users', 'pk' => 'users.id', 'fk' => 'lessons.user_id');

        $arrParams['conditions'][] = array('field' => 'lessons.status', 'opr' => '=', 'value' => $data['appends']['status']);
        if ($data['appends']['tone'] != 0)
            $arrParams['conditions'][] = array('field' => 'lessons.tone_id', 'opr' => '=', 'value' => $data['appends']['tone']);
        if ($data['appends']['category'] != 0)
            $arrParams['conditions'][] = array('field' => 'lessons.category_id', 'opr' => '=', 'value' => $data['appends']['category']);

        $arrParams['paginate']['appends'] = $data['appends'];

        $arrParams['paginate']['nums'] = 25;
        $arrParams['orders'] = array('field' => 'lessons.id', 'type' => 'DESC');
        $arrParams['selects'] = array('lessons.*', 'users.username');
        $data['lessons'] = $myModel->listItems('Lesson', $arrParams);
        return View::make('backend/lessons/index', compact('data'));
    }

    public function getCreate()
    {

    }

    public function getEdit($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['lesson', 'lesson.edit']))
            return View::make('backend/notallow');
        if (is_null($data['lesson'] = Lesson::find($id)))
            return Redirect::route('lessons/admin')->with('error', '');

        $data['topics'] = array();
        $data['topics'] = \Topic::returnArray();
        $data['topicables'] = array();
        $topicables = $data['lesson']->topics()->select('topic_id')->get();
        foreach ($topicables as $topicable) {
            $data['topicables'][] = $topicable->topic_id;
        }

        $myModel = new \MyModel();

        $data['categories'] = $myModel->listAllItemsToArray('Category', array('value' => 'id', 'display' => 'title'));
        $data['tones'] = $myModel->listAllItemsToArray('Tone', array('value' => 'id', 'display' => 'title'));
        $data['tags'] = $data['lesson']->tags()->select('tags.*')->get();
        return View::make('backend/lessons/create', compact('data'));
    }

    public function getDelete($id)
    {

    }

    public function getRestore($id)
    {

    }

    public function postEdit()
    {

    }
} 